package com.czy.daishusys.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czy.daishusys.R;

/**
 * @author: Raven
 * @date: 2017/11/28 8:26
 * @email: xiexiaomeng_001@163.com
 * @description: 带背景的Dialog
 */

public class PicDialog extends Dialog{

    public PicDialog(@NonNull Context context) {
        super(context);
    }

    public PicDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected PicDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public static class Builder {
        private Context context;
        private int titleIconSrcId;//顶部Icon
        private String title;//标题
        private String message;//内容信息
        private String positiveButtonText;
        private String negativeButtonText;
        private int positiveIconSrcId;//确定按钮图标
        private int negativeIconSrcId;//取消按钮图标
        private View contentView;
        private DialogInterface.OnClickListener positiveButtonClickListener;
        private DialogInterface.OnClickListener negativeButtonClickListener;

        public Builder(Context context){
            this.context = context;
        }

        /**
         * 设置对话框顶部ICON
         * @param resId
         * @return
         */
        public Builder setTitleIcon(int resId){
            this.titleIconSrcId = resId;
            return this;
        }

        /**
         * 设置对话框标题
         * @param title
         * @return
         */
        public Builder setTitle(String title){
            this.title = title;
            return this;
        }

        /**
         * 设置对话框信息
         * @param message
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        /**
         * 设置确认按钮的ICON
         * @param resId
         * @return
         */
        public Builder setPositiveIcon(int resId){
            this.positiveIconSrcId = resId;
            return this;
        }

        /**
         * 设置取消按钮的ICON
         * @param resId
         * @return
         */
        public Builder setNegativeIcon(int resId){
            this.negativeIconSrcId = resId;
            return this;
        }

        /**
         * 设置对话框监听
         * @param positiveButtonText
         * @param listener
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            MyLog.d("TEST","call setNegativeButton");
            return this;
        }

        public PicDialog create(){
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final PicDialog dialog = new PicDialog(context, R.style.AlertDialogStyle);
            View layout = inflater.inflate(R.layout.layout_pic_dialog, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            ImageView titleIcon = (ImageView) layout.findViewById(R.id.iv_pic_dialog_title_icon);
            TextView tvTitle = (TextView) layout.findViewById(R.id.tv_pic_dialog_title);
            TextView tvMessage = (TextView) layout.findViewById(R.id.tv_pic_dialog_message);

            LinearLayout llRoot = (LinearLayout) layout.findViewById(R.id.ll_root);
            LinearLayout llPositive = (LinearLayout) layout.findViewById(R.id.ll_positive);
            LinearLayout llNegative = (LinearLayout) layout.findViewById(R.id.ll_negative);

            TextView tvPositive = (TextView) layout.findViewById(R.id.tv_positive);
            TextView tvNegative = (TextView) layout.findViewById(R.id.tv_negative);

            if(titleIconSrcId > 0){
                titleIcon.setImageResource(titleIconSrcId);
            }

            if(message != null && !message.equals("")){
                tvMessage.setVisibility(View.VISIBLE);
                tvMessage.setText(message);
            }else {
                tvMessage.setVisibility(View.GONE);
            }

            /*如果俩个按钮均未设名称*/
            if(TextUtils.isEmpty(positiveButtonText) && TextUtils.isEmpty(negativeButtonText)){
                layout.findViewById(R.id.ll_bottom_btn).setVisibility(View.GONE);//隐藏两个按键
                layout.findViewById(R.id.tv_bottom_h_divider).setVisibility(View.GONE);//隐藏横向分割线
            } else {
                /*只设置了一个按钮*/
                if(TextUtils.isEmpty(positiveButtonText) || TextUtils.isEmpty(negativeButtonText)){
                    layout.findViewById(R.id.tv_bottom_v_divider).setVisibility(View.GONE);//隐藏竖向分割线
                }

                /*确定和取消按钮设置*/
                if (!TextUtils.isEmpty(positiveButtonText)) {
                    llPositive.setVisibility(View.VISIBLE);
                    tvPositive.setText(positiveButtonText);
                    if (positiveButtonClickListener != null) {
                        llPositive.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                                dialog.dismiss();
                            }
                        });
                    }

                    if (positiveIconSrcId > 0) {
                        Drawable drawable = context.getResources().getDrawable(positiveIconSrcId);
                        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                        tvPositive.setCompoundDrawables(drawable, null, null, null);
                    } else {
                        tvPositive.setCompoundDrawables(null, null, null, null);
                    }
                } else {
                    llPositive.setVisibility(View.GONE);
                }


                if (!TextUtils.isEmpty(negativeButtonText)) {
                    llNegative.setVisibility(View.VISIBLE);
                    tvNegative.setText(negativeButtonText);
                    if (negativeButtonClickListener != null) {
                        llNegative.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                MyLog.d("TEST", "call NegativeButton onclick");
                                negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                                dialog.dismiss();
                            }
                        });
                    }

                    if (negativeIconSrcId > 0) {
                        Drawable drawable = context.getResources().getDrawable(negativeIconSrcId);
                        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                        tvNegative.setCompoundDrawables(drawable, null, null, null);
                    } else {
                        tvNegative.setCompoundDrawables(null, null, null, null);
                    }
                } else {
                    llNegative.setVisibility(View.GONE);
                }
            }


            if(title != null && !title.equals("")){
                tvTitle.setVisibility(View.VISIBLE);
                tvTitle.setText(title);
            }else{
                tvTitle.setVisibility(View.GONE);
            }
            dialog.setContentView(layout);
            // 调整dialog背景大小
            WindowManager windowManager = (WindowManager) context
                    .getSystemService(Context.WINDOW_SERVICE);
            llRoot.setLayoutParams(new FrameLayout.LayoutParams((int) (windowManager.getDefaultDisplay()
                    .getWidth() * 0.85), LinearLayout.LayoutParams.WRAP_CONTENT));
            return dialog;
        }
    }


    /**
     * 测试代码， 不同方式展示的对话框
     */

    private void showTestDialog(){

//        PicDialog.Builder builder = new PicDialog.Builder(Context);

        /*显示1：有标题，标题图标，信息，取消和确定按钮
         builder.setTitleIcon(R.mipmap.ic_dialog_clear)
         .setTitle("清除缓存")
         .setMessage("确定要清理缓存吗？")
         .setNegativeIcon(R.mipmap.ic_dialog_cancel)
         .setPositiveIcon(R.mipmap.ic_dialog_ensure)
         .setNegativeButton("取消", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        }
        })
         .setPositiveButton("确定", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        }
        });*/

        /*显示2：标题当作信息，标题图标，无信息，取消和确定按钮，如打电话确认对话框
         builder.setTitleIcon(R.mipmap.ic_dialog_title_phone)
         .setTitle("13399778899")
         .setNegativeIcon(R.mipmap.ic_dialog_cancel)
         .setPositiveIcon(R.mipmap.ic_dialog_call)
         .setNegativeButton("取消", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        }
        })
         .setPositiveButton("呼叫", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        }
        });*/

        /*显示3：标题当作信息，标题图标，无信息，无按钮
         builder.setTitleIcon(R.mipmap.ic_dialog_title_sent)
         .setTitle("发货成功");*/

        /*显示4：标题当作信息，标题图标，无信息，只有确定按钮
         builder.setTitleIcon(R.mipmap.ic_dialog_title_sent)
         .setTitle("发货成功")
         .setPositiveIcon(R.mipmap.ic_dialog_ensure)
         .setPositiveButton("知道了", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        }
        });*/

        /*显示5：标题当作信息，标题图标，无信息，只有取消按钮
         builder.setTitleIcon(R.mipmap.ic_dialog_title_sent)
         .setTitle("测试")
         .setPositiveIcon(R.mipmap.ic_dialog_cancel)
         .setNegativeButton("取消", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        }
        });*/

//        builder.create().show();
    }
}
